package com.rc.saas.tenant.controller.system;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.msg.TenantNotice;
import com.rc.saas.tenant.model.system.RcAgreementNotification;
import com.rc.saas.tenant.search.rc.RcAgreementNotificationSearch;
import com.rc.saas.tenant.search.tenant.TenantUserSearch;
import com.rc.saas.tenant.service.system.RcAgreementNotificationService;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.tenant.TenantUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

@Controller
@RequestMapping("/sys/content")
public class RcAgreementNotificationController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RcAgreementNotificationService rcAgreementNotificationService;

    @RequestMapping("/index")
    public String index() {
        return "/templates/system/content";
    }

    @ResponseBody
    @RequestMapping(value = "/listPageContent", method = RequestMethod.GET)
    public WebPageInfo listPageContent(@RequestParam(value = "title", required = false)String title,
                                     @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo,
                                     @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize) {
        PageInfo pageInfo = rcAgreementNotificationService.listPageContent(title, pageNo, pageSize);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo ;
    }

    @ResponseBody
    @RequestMapping(value = "/udpateContentStatus", method = RequestMethod.POST)
    public TResponse udpateContentStatus(Long id, String status) {
        RcAgreementNotification agreementNotification = rcAgreementNotificationService.selectByPrimaryKey(id);
        agreementNotification.setStatus(status);
        rcAgreementNotificationService.updateByPrimaryKey(agreementNotification);
        return TResponse.SUCCESS();
    }

    @RequestMapping("/add")
    public String add() {
        return "/templates/system/addContent";
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model,@PathVariable("id") Long id) {
        RcAgreementNotification agreementNotification = rcAgreementNotificationService.selectByPrimaryKey(id);
        model.addAttribute("content", agreementNotification);
        return "/templates/system/editContent";
    }

    @ResponseBody
    @RequestMapping(value = "/saveContent", method = RequestMethod.POST)
    public TResponse saveContent(RcAgreementNotification agreementNotification) {
        rcAgreementNotificationService.updateByPrimaryKeySelective(agreementNotification);
        return TResponse.SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/addContent", method = RequestMethod.POST)
    public TResponse addContent(RcAgreementNotification agreementNotification) {
        rcAgreementNotificationService.insertSelective(agreementNotification);
        return TResponse.SUCCESS();
    }


    /**
     * 导出内容设置列表
     */
    @ResponseBody
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public TResponse export(RcAgreementNotificationSearch rcAgreementNotificationSearch, HttpServletResponse response) {
        try {
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            ExportUtils.setDefaultPaging(rcAgreementNotificationSearch);
            PageInfo<RcAgreementNotification> page = rcAgreementNotificationService.listPageContentAll(rcAgreementNotificationSearch);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("内容设置列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), RcAgreementNotification.class).sheet("内容设置列表").doWrite(page.getList());
            return null;
        } catch (Exception e) {
            logger.error("导出内容设置列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }



}
